import { useEffect, useState } from "react";
export const useTitle = () => {
  useEffect(() => {
    document.title = "测试自定义hooks";
  }, []);
};

//获取鼠标位置
export const getMousePosition = () => {
  const [x, setx] = useState(0);
  const [y, sety] = useState(0);
  function mouseHander(e: MouseEvent) {
    const x = e.clientX;
    const y = e.clientY;
    setx(x);
    sety(y);
  }
  useEffect(() => {
    window.addEventListener("mousemove", mouseHander);
    return () => {
      //销毁监听事件
      window.removeEventListener("mousemove", mouseHander);
      //返回一个函数
      console.log("getMousePosition监听销毁成功");
    };
  }, []);
  return { x, y };
};
//异步获取的数据

// function getinfo(): Promise<string> {
//   return new Promise((resolve) => {
//     setTimeout(() => {
//       resolve("获得的数据");
//     }, 1500);
//   });
// }

const getinfo = (): Promise<string> => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve("获得的数据");
    }, 1500);
  });
};

export const useGetinfo = () => {
  const [flag, setflag] = useState(false);
  const [info, setinfo] = useState("");
  getinfo().then((res: string) => {
    setflag(true);
    setinfo(res);
  });
  return { flag, info };
};
